package shiyanbaogao6;



import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class EmployeeManager {
    private List<Employee> employees = new ArrayList<>();

    // 添加员工信息到文件中
    public void addEmployeeToFile(Employee employee, String filePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
            writer.write(employee.getName() + "," + employee.getAge() + "," + employee.getSalary());
            writer.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 从文件中读取所有员工信息
    public List<Employee> readEmployeesFromFile(String filePath) {
        List<Employee> employees = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length == 3) {
                    String name = parts[0];
                    int age = Integer.parseInt(parts[1]);
                    double salary = Double.parseDouble(parts[2]);
                    employees.add(new Employee(name, age, salary));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return employees;
    }

    // 打印所有员工信息到控制台
    public void printEmployees(List<Employee> employees) {
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }
}


